package org.hitchhike.common.service;

import java.io.Serializable;

import org.hitchhike.common.repository.BaseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

public class BaseService<T, ID extends Serializable> {

  @Autowired
  protected BaseRepository<T, ID> baseRepository;

  @Transactional(rollbackFor = {Exception.class})
  public T save(T t) {
    return baseRepository.save(t);
  }

  @Transactional(rollbackFor = {Exception.class})
  public T saveAndFlush(T t) {
    save(t);
    baseRepository.flush();
    return t;
  }

  @Transactional(rollbackFor = {Exception.class})
  public T update(T t) {
    return baseRepository.save(t);
  }

  @Transactional(rollbackFor = {Exception.class})
  public void delete(ID id) {
    baseRepository.delete(id);
  }

  @Transactional(rollbackFor = {Exception.class})
  public void delete(T t) {
    baseRepository.delete(t);
  }

  public T get(ID id) {
    return baseRepository.findOne(id);
  }

}
